package command
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import event.LoginRequest;
	
	import model.Model;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import services.databasewrapper.DatabaseWrapper;
	
	import valueObjects.VOUser;
	
	public class LoginCommand implements IResponder, Command
	{
		/************************************
		 *    Consatnts
		 ************************************/
		private static var INVALID_PASSWORD:String = "invalid password";
		private static var INVALID_USERNAME:String = "invalid username";
		
		/**
		 * 	model
		 */ 
		public var tripModel : Model = Model.instance;
		
		public function LoginCommand()
		{
		}
		
		public function execute(e:CairngormEvent):void
		{
			trace ("Login request...");
			var databaseWrapper:DatabaseWrapper = new DatabaseWrapper();
			databaseWrapper.addEventListener(FaultEvent.FAULT, fault);
			databaseWrapper.addEventListener(ResultEvent.RESULT, result);			
			databaseWrapper.login((e as LoginRequest).additionalData.username, 
								  (e as LoginRequest).additionalData.password);
		}
		
		public function result(data:Object):void
		{
			var user:VOUser= new VOUser();
			
			if ( data == null || !data.result)
			{
				tripModel.isLoggedIn= false;
				return;
			}
			else
			{
				
				trace(data.result.id);
				trace(data.result.username);
				trace(data.result.password);
				trace(data.result.age);
				trace(data.result.address);
				trace(data.result.photo);
				trace(data.result.mail);
				trace(data.result.name);
				
				//var user:VOUser = data.result as VOUser;
				user.id = data.result.id;
				user.username = data.result.username;
				user.password = data.result.password;
				user.age = data.result.age;
				user.address = data.result.address;
				user.photo = data.result.photo;
				user.mail = data.result.mail;
				user.name = data.result.name;
				user.lastname = data.result.lastname;
				trace(user.lastname);
				
				//user = data.result as VOUser;
				
				if (user)
				{
					/*switch (user.password)
					{
						case INVALID_PASSWORD:
						case INVALID_USERNAME:
							Alert.show("Invalid username or password, please type again or register!");
							break;
						default:
							tripModel.currentUser = user;
							tripModel.isLoggedIn = true;
							break;
					}*/
					if(user.username== INVALID_USERNAME)
					{
						Alert.show("Invalid username, please type again or register!");
					}
					else  
					{
						if(user.password== INVALID_PASSWORD )
							Alert.show("Password Incorrect, please type again or register!");
						else
						{	
							tripModel.currentUser = user;
							tripModel.isLoggedIn = true;
						}	
					}
				
				
				
				}
				else
				{
					tripModel.isLoggedIn= false;
					Alert.show("Invalid username or password, please type again or register!");
				}
			}
			
		}
		
		public function fault(info:Object):void
		{
		}
		
	}
}